package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
import models.Staff;
import models.Vehicle;
@Entity
@Table(name="part_consume")
public class Part_consume extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	
	public String material; //材料名称、型号、品牌
	
	public Double unit_price; //单价
	
	public Integer invoice_number; //发票号/收据单号
	
	public Integer inventory; //存库数量
	
	public Integer consume_quantity; //领用数量
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date consume_day; //领料日期
	@ManyToOne
	public Staff consume_person; //领料人
	
	public Integer consume_number; //领料单号
	@ManyToOne
	public Staff ship_person; //发货人
	
	public String use_type; //用途属性
	@ManyToOne
	public Vehicle vehicle; //领用车号
	
	public String comments; //领用说明
	public static Model.Finder<Integer, Part_consume> find = new Model.Finder<Integer, Part_consume>(Integer.class, Part_consume.class);

	public static Page<Part_consume> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Part_consume> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setMaterial(String d) {
		material= d;
	}
	public String getMaterial() {
		return material;
	}
	public void setUnit_price(Double d) {
		unit_price= d;
	}
	public Double getUnit_price() {
		return unit_price;
	}
	public void setInvoice_number(Integer d) {
		invoice_number= d;
	}
	public Integer getInvoice_number() {
		return invoice_number;
	}
	public void setInventory(Integer d) {
		inventory= d;
	}
	public Integer getInventory() {
		return inventory;
	}
	public void setConsume_quantity(Integer d) {
		consume_quantity= d;
	}
	public Integer getConsume_quantity() {
		return consume_quantity;
	}
	public void setConsume_day(Date d) {
		consume_day= d;
	}
	public Date getConsume_day() {
		return consume_day;
	}
	public void setConsume_person(Staff d) {
		consume_person= d;
	}
	public Staff getConsume_person() {
		return consume_person;
	}
	public void setConsume_number(Integer d) {
		consume_number= d;
	}
	public Integer getConsume_number() {
		return consume_number;
	}
	public void setShip_person(Staff d) {
		ship_person= d;
	}
	public Staff getShip_person() {
		return ship_person;
	}
	public void setUse_type(String d) {
		use_type= d;
	}
	public String getUse_type() {
		return use_type;
	}
	public void setVehicle(Vehicle d) {
		vehicle= d;
	}
	public Vehicle getVehicle() {
		return vehicle;
	}
	public void setComments(String d) {
		comments= d;
	}
	public String getComments() {
		return comments;
	}
	public static Part_consume findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Part_consume v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
}
